package com.springboot.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.base.common.BusinessException;
import com.springboot.base.common.EmBusinessError;
import com.springboot.base.dal.CategoryMapper;
import com.springboot.base.dal.SellerMapper;
import com.springboot.base.model.Category;
import com.springboot.base.model.Seller;
import com.springboot.base.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public IPage<Category> qryListByPage(Page<Category> page) {
        IPage<Category> list = categoryMapper.qryListByPage(page);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Category create(Category categoryModel) throws BusinessException {
        categoryModel.setCreatedAt(new Date());
        categoryModel.setUpdatedAt(new Date());
        try{
            categoryMapper.insert(categoryModel);
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.BIND_ERROR);
        }
        return categoryMapper.selectById(categoryModel.getId());
    }

    @Override
    public List<Category> selectAll(){
        return categoryMapper.selectAll();
    }

    @Override
    public int countAllCategory() {
        return categoryMapper.selectCount(new QueryWrapper<Category>());
    }


}
